/**
 * @File AccountMasterController.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date March 25, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.purchase.accountMaster;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;

@Controller
public class AccountMasterController {

	@Autowired
	@Qualifier("AccountMasterDaoImpl")
	private AccountMasterDao accountMasterDao;
	
	public List<AccountMasterInfoBean> getCompanies(){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setUniversityCode("0001");
		List<AccountMasterInfoBean> companyList = new ArrayList<AccountMasterInfoBean>();
		companyList = accountMasterDao.getCompanyDetails(input);
		return companyList;
	}
	
	public List<AccountMasterInfoBean> getAccountCharts(){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setUniversityCode("0001");
		List<AccountMasterInfoBean> chartList = new ArrayList<AccountMasterInfoBean>();
		chartList = accountMasterDao.getAccountChartDetails(input);
		return chartList;
	}
	
	public List<AccountMasterInfoBean> getAccounts(String companyCode, String chartCode){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setCompanyCode(companyCode);
		input.setChartCode(chartCode);
		input.setUniversityCode("0001");
		List<AccountMasterInfoBean> accountsList = new ArrayList<AccountMasterInfoBean>();
		accountsList = accountMasterDao.getAccountMasterDetails(input);
		return accountsList;
	}
	
	public List<AccountMasterInfoBean> getAccountTypes(){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setUniversityCode("0001");
		List<AccountMasterInfoBean> accountTypeList = new ArrayList<AccountMasterInfoBean>();
		accountTypeList = accountMasterDao.getAccountTypeDetails(input);
		return accountTypeList;
	}
	
	public String setAccounts(Map<String, String> obj){
		String accountCode = "";
		FlexSession session = FlexContext.getFlexSession();
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setAccountCode(obj.get("accountCode"));
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountName(obj.get("accountName"));
		input.setAccountColor(obj.get("accountColor"));
		input.setAccountDescription(obj.get("accountDescription"));
		input.setAccountNotes(obj.get("accountNotes"));
		input.setInitialBalance(Double.parseDouble(obj.get("initialBalance")));
		input.setInitialTransferAccount(obj.get("initialTransferAccount"));
		input.setPrimaryAccount(obj.get("primaryAccount"));
		input.setAccountType(obj.get("accountType"));
		input.setParentAccountCode(obj.get("parentAccountCode"));
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		input.setCreatorId(session.getAttribute("creatorId").toString());
		accountCode = accountMasterDao.createNewAccount(input);
		return accountCode;
	}
	
	public AccountMasterInfoBean getAccountDetails(Map<String, String> obj){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		AccountMasterInfoBean output = new AccountMasterInfoBean();
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountCode(obj.get("accountCode"));
		input.setUniversityCode("0001");
		output = accountMasterDao.getAccountDetailsForEditing(input);
		return output;
	}
	
	public boolean updateAccount(Map<String, String> obj){
		boolean flag = false;
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setAccountCode(obj.get("accountCode"));
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountName(obj.get("accountName"));
		input.setAccountColor(obj.get("accountColor"));
		input.setAccountDescription(obj.get("accountDescription"));
		input.setAccountNotes(obj.get("accountNotes"));
		input.setInitialBalance(Double.parseDouble(obj.get("initialBalance")));
		input.setInitialTransferAccount(obj.get("initialTransferAccount"));
		input.setPrimaryAccount(obj.get("primaryAccount"));
		input.setAccountType(obj.get("accountType"));
		input.setParentAccountCode(obj.get("parentAccountCode"));
		input.setUniversityCode("0001");
		input.setModifierId("Dheeraj");
		flag = accountMasterDao.updateAccountProperties(input);
		return flag;
	}
	
	public String deleteAccount(Map<String, String> obj){
		String message = "";
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setAccountCode(obj.get("accountCode"));
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setUniversityCode("0001");
		message = accountMasterDao.deleteAccount(input);
		return message;
	}
	
	public String setGroupAccounts(Map<String, String> obj){
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		FlexSession session = FlexContext.getFlexSession();
		String accountCode = "";
		
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountName(obj.get("accountName"));
		input.setAccountColor(obj.get("accountColor"));
		input.setAccountDescription(obj.get("accountDescription"));
		input.setAccountNotes(obj.get("accountNotes"));
		input.setPrimaryAccount(obj.get("primaryAccount"));
		input.setAccountType(obj.get("accountType"));
		input.setParentAccountCode(obj.get("parentAccountCode"));
		input.setAccountTypeCode(obj.get("codeStartFrom"));
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		input.setCreatorId(session.getAttribute("creatorId").toString());
		accountCode = accountMasterDao.createNewGroupAccount(input);
		return accountCode;
	}
	
	public List<AccountMasterInfoBean> getGroupAccounts(String companyCode, String chartCode){
		FlexSession session = FlexContext.getFlexSession();
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setCompanyCode(companyCode);
		input.setChartCode(chartCode);
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		List<AccountMasterInfoBean> accountsList = new ArrayList<AccountMasterInfoBean>();
		accountsList = accountMasterDao.getGroupAccountDetails(input);
		return accountsList;
	}
	
	public AccountMasterInfoBean getGroupAccountDetails(Map<String, String> obj){
		FlexSession session = FlexContext.getFlexSession();
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		AccountMasterInfoBean output = new AccountMasterInfoBean();
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountCode(obj.get("accountCode"));
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		output = accountMasterDao.getGroupAccountDetailsForEditing(input);
		return output;
	}
	
	public boolean updateGroupAccount(Map<String, String> obj){
		boolean flag = false;
		FlexSession session = FlexContext.getFlexSession();
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setAccountCode(obj.get("accountCode"));
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setAccountName(obj.get("accountName"));
		input.setAccountColor(obj.get("accountColor"));
		input.setAccountDescription(obj.get("accountDescription"));
		input.setAccountNotes(obj.get("accountNotes"));
		input.setPrimaryAccount(obj.get("primaryAccount"));
		input.setAccountType(obj.get("accountType"));
		input.setParentAccountCode(obj.get("parentAccountCode"));
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		input.setModifierId(session.getAttribute("modifierId").toString());
		flag = accountMasterDao.updateGroupAccountProperties(input);
		return flag;
	}
	
	public String deleteGroupAccount(Map<String, String> obj){
		String message = "";
		FlexSession session = FlexContext.getFlexSession();
		AccountMasterInfoBean input = new AccountMasterInfoBean();
		input.setAccountCode(obj.get("accountCode"));
		input.setCompanyCode(obj.get("companyCode"));
		input.setChartCode(obj.get("chartCode"));
		input.setParentAccountCode(obj.get("parentAccountCode"));
		input.setUniversityCode(session.getAttribute("universityCode").toString());
		message = accountMasterDao.deleteGroupAccount(input);
		return message;
	}
	
}
